package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.common.ThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    FileStorageService fileStorageService;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        //文件流
        try {
            InputStream inputStream = multipartFile.getInputStream();
            String filename = multipartFile.getOriginalFilename();
            //文件上传minio
            //pic4.png ->12121212.png
            //优化部分 文件重名问题 时间戳 纳秒
            String substring = StringUtils.substring(filename, filename.lastIndexOf("."));
            String uploadImgFile = fileStorageService.uploadImgFile("", System.nanoTime()+substring, inputStream);
           // System.out.println(uploadImgFile);

            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUserId(ThreadLocalUtil.getUserId());
            wmMaterial.setUrl(uploadImgFile);
            wmMaterial.setCreatedTime(new Date());
            this.save(wmMaterial);
        } catch (IOException e) {
           // e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"文件上传失败");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
       //select * from wm_matral where isColltion=0/1
        Page<WmMaterial> page = new Page<>();
        page.setSize(dto.getSize());
        page.setCurrent(dto.getPage());
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        queryWrapper.eq(WmMaterial::getUserId,ThreadLocalUtil.getUserId());
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);
        page= this.page(page, queryWrapper);

        PageResponseResult pageResponseResult =new PageResponseResult(page);
        pageResponseResult.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        pageResponseResult.setErrorMessage(AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return pageResponseResult;
    }
}
